local skel = fk.CreateSkill {
  name = "emo__yindie",
}

Fk:loadTranslationTable{
  ["emo__yindie"] = "引蝶",
  [":emo__yindie"] = "出牌阶段限两次，你可以与一名其他角色依次重铸两张牌，然后你与其依次可以使用一张摸到的牌，以获得一张重铸牌。",

  ["#emo__yindie"] = "引蝶：选择两张牌并选择一名其他角色，与其重铸两张牌",
  ["#emo__yindie-recast"] = "引蝶：请重铸两张牌",
  ["#emo__yindie-use"] = "引蝶：你可以使用一张摸到的牌！",
}

skel:addEffect("active", {
  anim_type = "support",
  prompt = "#emo__yindie",
  card_num = 2,
  target_num = 1,
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    return #selected_cards == 2 and #selected == 0 and to_select ~= player and #to_select:getCardIds("he") > 1
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected < 2
  end,
  times = function (self, player)
    return 2 - player:usedSkillTimes(skel.name, Player.HistoryPhase)
  end,
  can_use = function (self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) < 2
  end,
  on_use = function (self, room, use)
    local player, to = use.from, use.tos[1]
    local recast = table.simpleClone(use.cards)
    local my_draw = room:recastCard(recast, player, skel.name)
    local to_draw = {}
    local function getDisCard(p)
      recast = table.filter(recast, function (id) return room:getCardArea(id) == Card.DiscardPile end)
      if #recast > 0 then
        local cid = room:askToChooseCard(p, {
          target = p, skill_name = skel.name, prompt = "#emo-getcard",
          flag = { card_data = { { skel.name, recast } } }
        })
        room:obtainCard(p, cid, false, fk.ReasonPrey, p, skel.name)
      end
    end
    if not to.dead and not to:isNude() then
      local cards = to:getCardIds("he")
      if #cards > 2 then
        cards = room:askToCards(to, {
          min_num = 2, max_num = 2, include_equip = true, cancelable = false, skill_name = skel.name,
          prompt = "#emo__yindie-recast",
        })
      end
      table.insertTable(recast, cards)
      to_draw = room:recastCard(cards, to, skel.name)
    end
    my_draw = table.filter(my_draw, function (id) return table.contains(player:getCardIds("h"), id) end)
    if not player.dead and #my_draw > 0 then
      if room:askToUseRealCard(player, {
        pattern = my_draw, skill_name = skel.name, prompt = "#emo__yindie-use",
        extra_data = {bypass_times = true},
      }) and player:isAlive() then
        getDisCard(player)
      end
    end
    to_draw = table.filter(to_draw, function (id) return table.contains(to:getCardIds("h"), id) end)
    if to:isAlive() and #to_draw > 0 then
      if room:askToUseRealCard(to, {
        pattern = to_draw, skill_name = skel.name, prompt = "#emo__yindie-use",
        extra_data = {bypass_times = true},
      }) and to:isAlive() then
        getDisCard(to)
      end
    end
  end,
})

return skel
